package main

import (
	"embed"
	_ "embed"
	"log"
	"quant/backend/conf"
	"quant/backend/constants"
	"quant/backend/manager"
	"quant/backend/service"
	"time"

	"github.com/wailsapp/wails/v3/pkg/application"
)

// Wails uses Go's `embed` package to embed the frontend files into the binary.
// Any files in the frontend/dist folder will be embedded into the binary and
// made available to the frontend.
// See https://pkg.go.dev/embed for more information.

//go:embed all:frontend/dist
var assets embed.FS

// main function serves as the application's entry point. It initializes the application, creates a window,
// and starts a goroutine that emits a time-based event every second. It subsequently runs the application and
// logs any error that might occur.
func main() {
	conf.Init()
	// Create a new Wails application by providing the necessary options.
	// Variables 'Name' and 'Description' are for application metadata.
	// 'Assets' configures the asset server with the 'FS' variable pointing to the frontend files.
	// 'Bind' is a list of Go struct instances. The frontend has access to the methods of these instances.
	// 'Mac' options tailor the application when running an macOS.
	constants.App = application.New(application.Options{
		Name:        "quant-wails",
		Description: "A demo of using raw HTML & CSS",
		Services: []application.Service{
			application.NewService(manager.NewConfigManager()),
			application.NewService(manager.NewStockManager()),
			application.NewService(manager.NewDailyManager()),
			application.NewService(service.NewGreetService()),
			application.NewService(service.NewMarketService()),
			application.NewService(service.NewTradeService()),
		},

		Assets: application.AssetOptions{
			Handler: application.AssetFileServerFS(assets),
		},
		Mac: application.MacOptions{
			ApplicationShouldTerminateAfterLastWindowClosed: true,
		},
	})
	// Create a new window with the necessary options.
	// 'Title' is the title of the window.
	// 'Mac' options tailor the window when running on macOS.
	// 'BackgroundColour' is the background colour of the window.
	// 'URL' is the URL that will be loaded into the webview.
	constants.App.Window.NewWithOptions(application.WebviewWindowOptions{
		Title:     "量化辅助",
		Width:     1024,
		Height:    768,
		MinWidth:  1024,
		MinHeight: 768,
		// Frameless: true,
		// MaxHeight: 800,
		// MaxWidth:  1280,
		// DisableResize: true,
		Mac: application.MacWindow{
			InvisibleTitleBarHeight: 50,
			// Backdrop:                application.MacBackdropTranslucent,
			// TitleBar: application.MacTitleBarDefault,
			TitleBar: application.MacTitleBarHidden,
		},
		BackgroundColour: application.NewRGB(27, 38, 54),
		URL:              "/",
	})

	// Create a goroutine that emits an event containing the current time every second.
	// The frontend can listen to this event and update the UI accordingly.
	go func() {
		for {
			now := time.Now().Format(time.DateTime)
			constants.App.Event.Emit("time", now)
			time.Sleep(time.Second)
		}
	}()
	// constants.App = app
	// Run the application. This blocks until the application has been exited.
	err := constants.App.Run()
	// If an error occurred while running the application, log it and exit.
	if err != nil {
		log.Fatal(err)
	}
}
